class: title-slide, right, top background-image: url(data:image/png;base64,#img/dawn.png) background-position: 90% 75%, 75% 75% background-size:cover .left-column[ # NHS Workshop<br>Introduction to ggplot ] .right-column[ ### colours and facets **Eugene Hickey**<br> January 21st 2021 ] .palegrey[.left[.footnote[Graphic by [Elaine Hickey](https://photos.google.com/photo/AF1QipMjKNoaxyne8nte4HmxA6Th9-4fUfSbl_mx-_1G)]]] ??? Welcome to the workshop on ggplot. Where we'll show you how to create impressive data visualisations. --- layout: true <a class="footer-link" href="http://intro-ggplot-nhs.netlify.app">intro-ggplot-nhs — Eugene Hickey</a> <!-- this adds the link footer to all slides, depends on footer-link class in css--> --- # Choice of Colours in R <br> ### We'll also discuss faceting. - colours are very important - second only to position for perception - can carry information - also important to be visually pleasing - worthwhile to make your figures aesthetically attractive - visualisations that are engaging are more effective --- ### Types of Colour Scales .panelset[ .panel[ .panel-name[Qualitative] .pull-left[ - suite of colours easily distinguished - no heirarchy - caters for visual impairments ] .pull-right[ <!-- --> ] ]<!-- close first panel --> .panel[ .panel-name[Sequential] .pull-left[ - band of colours, increasingly intense - go from low to high ] .pull-right[ <!-- --> ] ]<!-- close second panel --> .panel[ .panel-name[Diverging] .pull-left[ - suite of colours from minus to plus - contrasting colours at each end - something neutral in the middle ] .pull-right[ <!-- --> ] ]<!-- close third panel --> ]<!-- close panelset --> --- # Getting Colours in R - some really great packages - <span style='color: #B03A2E'>RColorBrewer</span> - excellent, fine control over palette choice - <span style='color: #B03A2E'>viridis</span> - excels at palettes for vision-impaired readers - <span style='color: #B03A2E'>paletteer</span> - collection of palettes from various sources - <span style='color: #B03A2E'>wesanderson</span> - <i>names(wes_palettes)</i> followed by <i>wes_palette("BottleRocket1")</i> - <span style='color: #B03A2E'>rtist</span> - lifts principle colours from paintings --- ```r library(rtist) par(mfrow = c(3, 5)) map(names(rtist_palettes), function(x) print(rtist_palette(x))) ```  --- - more.... - tvthemes() - not just colours, but layouts and fonts - everything from Game of Thrones to Spongebob (yes, really) - ggsci(), palettes for scientific publications (Lancet, AAAS, etc) - colorspace() - resources for picking colours - choose_color() and choose_palette() - can convert colours based on vision deficiencies - will convert from colour descriptions, e.g. hex2RGB() - and a [cheatsheet](https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf) ---
--- ## Ways of Describing Colours - by name: "red", "cyan", "violetred4", "thistle"..... - get full list of 657 available in R from _colors()_ - by hex code: "#f49340", "#40f9f9", "#ee82ef", "#d8bfd1".... (see [htmlcolors](https://htmlcolorcodes.com/)]) - by rgb values: (249, 67, 64), (64, 249, 249), (57, 14, 30), (216, 191, 209).... - note, rgb() function takes these as a fraction from 0-1 - by hcl values: (53.24, 179.04, 12.17), (91.11, 72.10, 192.17), (32.36, 63.11, 349.86), (80.08, 20.79, 307.73).... --- # Investigating Colours in R - the following code shows the first "N" colours in R where N is set to 10 here: .pull-left[ ```r N <- 10 data.frame(col = colors()[1:N]) %>% ggplot(aes(x = col, fill = col)) + geom_bar(position = "stack", show.legend = F) + coord_flip() + theme_minimal() + theme(axis.text.x = element_blank(), axis.title.x = element_blank(), axis.text.y = element_blank()) ``` ] .pull-right[ <!-- --> ] --- ## Other Usful Functions - _show_col()_ from the _scales_ package is super useful - e.g. show_col("red") or show_col("#84a412") - *rgb()* will give a hex code for a fraction of red, green, blue - e.g. rgb(0.4, 0.2, 0.5) gives "#663380" --- - *colourPicker()* from the colourpicker package - colourPicker(numCols = 4), opens up shiny app, returns colours - *col2rgb()*, also *col2hex()* from the _gplots_ (not _ggplot2_) package, and *col2hcl* from the _jmw86069/jamba_ package - this last is on github, so you must install the package _devtools_ then do _install_github( jmw86069/jamba)_ - _colorfindr_ takes an image and identifies major colours --- .pull-left[  ] .pull-right[ ``` ## # A tibble: 6 x 3 ## col_hex col_freq col_share ## <chr> <int> <dbl> ## 1 #F9C6CB 1511 0.0300 ## 2 #7183C1 508 0.0101 ## 3 #268EB3 259 0.00515 ## 4 #AFB7DE 244 0.00485 ## 5 #6F81BF 99 0.00197 ## 6 #FAC7CC 99 0.00197 ``` <!-- --> ] --- # Some Websites and Tools - [coolors.co](coolors.co) - will generate appropriate palettes - [colorpicker](http://tristen.ca/hcl-picker/#/hlc/6/1.1/8C4443/845128) - [colorspace](http://colorspace.r-forge.r-project.org/articles/hcl_palettes.html#qualitative-palettes) - Chrome has an _**Eye Dropper**_ tool - click on part of a webpage and it will tell you the colour - Nice description of colurs from [Stowers](https://www.uv.es/conesa/CursoR/material/UsingColorInR.pdf) --- # Colours in _ggplot()_ - use for _fill_ and for _col_ aesthetics - add the _scale_fill_... and _scale_color_... layers to control - explore these by typing _?scale_fill_ and then TAB to see the range of options --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_brewer(type = 'qual') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_brewer(type = 'div') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_viridis_d(option = 'magma') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('rtist::warhol') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('MapPalettes::irish_flag') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('yarrr::southpark') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('dutchmasters::pearl_earring') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('dutchmasters::view_of_Delft') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('ghibli::KikiLight') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('LaCroixColoR::Coconut') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('LaCroixColoR::PassionFruit') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('ggsci::lanonc_lancet') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('ggsci::default_uchicago') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] --- count: false .panel1-my_rotate-rotate[ ```r dslabs::gapminder %>% group_by(continent, year) %>% summarise(average_fertility = mean(fertility, na.rm = TRUE)) %>% ungroup() %>% ggplot(aes(x = year, y = average_fertility, col = continent)) + geom_line(size = 2) + * scale_color_paletteer_d('ggsci::uniform_startrek') ``` ] .panel2-my_rotate-rotate[ <!-- --> ] <style> .panel1-my_rotate-rotate { color: black; width: 58.8%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-my_rotate-rotate { color: black; width: 39.2%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-my_rotate-rotate { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- count: false .panel1-histogram-colours-user[ ```r *dslabs::gapminder %>% * ggplot(aes(x = life_expectancy, * fill = ..x..)) + * geom_histogram() + * scale_fill_continuous(type = 'viridis') ``` ] .panel2-histogram-colours-user[ <!-- --> ] --- count: false .panel1-histogram-colours-user[ ```r dslabs::gapminder %>% ggplot(aes(x = life_expectancy, fill = ..x..)) + geom_histogram() + scale_fill_continuous(type = 'viridis') + * scale_fill_gradient2(low = "darkgreen", * mid = "white", * high = "firebrick4", * midpoint = 65) ``` ] .panel2-histogram-colours-user[ <!-- --> ] <style> .panel1-histogram-colours-user { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel2-histogram-colours-user { color: black; width: 49%; hight: 32%; float: left; padding-left: 1%; font-size: 80% } .panel3-histogram-colours-user { color: black; width: NA%; hight: 33%; float: left; padding-left: 1%; font-size: 80% } </style> --- class: center, inverse # _faceting_ - Faceting means producing multiple panels of a plot - Splits a plot into several versions based on a categorical variable - functions _facet_wrap()_ and _facet_grid()_ - useful when lots of data in different subsets - important to keep axis scales the same --- ```r sex <- c("Female", "Male") names(sex) <- c("F", "M") cats %>% ### cats dataset from MASS ggplot(aes(Bwt, Hwt)) + geom_point() + geom_smooth(aes(col = Sex), show.legend = F, se = F) + facet_grid(~Sex, labeller = labeller(Sex = sex)) + labs(x = "Bodyweight (kg)", y = "Heart Weight (g)") + theme_minimal() + theme(strip.background = element_blank(), text = element_text(size = 12)) ``` --- <img src="data:image/png;base64,#04-colours_files/figure-html/facet_example1-out-1.png" width="75%" height="40%" /> --- ```r movielens %>% ### movielens dataset from dslabs mutate(genres = str_replace(genres, pattern = "\\|.*", "")) %>% filter(genres != "(no genres listed)") %>% ggplot(aes(rating)) + stat_density(position = "identity", geom = "line", adjust = 3) + facet_wrap(.~genres, strip.position = "bottom", nrow = 3) + theme_minimal() + theme(text = element_text(size = 14), axis.text.y = element_blank(), axis.title.y = element_blank(), axis.text.x = element_blank(), strip.text = element_text(family = "Ink Free", face = "bold", size = 14, colour = "firebrick4"), axis.title.x = element_blank()) ``` --- <img src="data:image/png;base64,#04-colours_files/figure-html/facet_example2-out-1.png" width="75%" height="40%" /> --- ```r snails %>% ## snails dataset from the MASS library ggplot(aes(Exposure, Deaths, col = factor(Rel.Hum))) + facet_grid(Species ~ Temp) + geom_line() + geom_point() + theme_minimal() + theme(legend.title = element_blank(), text = element_text(size = 14)) ``` --- <img src="data:image/png;base64,#04-colours_files/figure-html/facet_example3-out-1.png" width="75%" height="40%" />